package cn.itsource.user.controller;

import cn.itsource.user.domain.User;
import cn.itsource.user.domain.dto.LogininfoDto;
import cn.itsource.user.service.ILogininfoService;
import cn.itsource.user.service.IUserService;
import cn.itsource.basic.util.AjaxResult;
import com.sun.org.apache.xpath.internal.SourceTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService service;
    @Autowired
    private ILogininfoService iLogininfoService;


    @PostMapping("/checkPhone")
    public AjaxResult checkPhone(@RequestBody User user){
        try {
            System.out.println(user.getPhone());
            service.checkPhone(user.getPhone());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     * 用户注册
     * @param logininfoDto
     * @return
     */
    @PostMapping("/register")
    public AjaxResult register(@RequestBody LogininfoDto logininfoDto){

        try {
            iLogininfoService.register(logininfoDto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }
    /**
     * 用户登录
     * @param logininfoDto
     * @return
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LogininfoDto logininfoDto){
        try {
            return iLogininfoService.login(logininfoDto);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }
}
